package net.huashitong.supervision.gongjian.service.project;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.supervision.gongjian.dao.ZhengQianDao;
import net.huashitong.supervision.gongjian.entity.project.ZhengQianEntity;
import net.huashitong.supervision.gongjian.filter.ZhengQianFilter;
import net.huashitong.supervision.gongjian.service.GjDeptProjectService;
import net.huashitong.supervision.gongjian.service.ProjectFeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Table;
import java.util.List;

@Service
public class ZhengQianService  extends StringPKService<ZhengQianEntity> {

    @Autowired
    private ZhengQianDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private GjDeptProjectService deptProjectService;
    @Autowired
    private ProjectFeedBackService feedBackService;

    @Override
    protected BaseDao<ZhengQianEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(ZhengQianEntity obj) {
        String sysId = tableService.updateMaxSysId("t_gj_zhengqianjiaodi", "");
        obj.setSysId(sysId);
    }

    /**
     * 当期填报状态
     * @param entity
     * @param vaild
     * @param pType
     * @return
     */
    private  ZhengQianEntity setState(ZhengQianEntity entity,int vaild ,int pType){
        int feedbackCount = feedBackService.getFeedbackCount(entity.getSysId(), vaild, pType);
        if(1<=feedbackCount){
            entity.setCurrentBack(1);
        }else {
            entity.setCurrentBack(0);
        }
        return entity;
    }

    /**
     * 不带单位过滤
     * @param filter
     * @return
     */
    public List<ZhengQianEntity> getList(ZhengQianFilter filter){


        List<ZhengQianEntity> list = dao.getList(filter);
        for (ZhengQianEntity item : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(item.getSysId(), 1, 1);
            item.setPeiheDept(projectDeptName);
            String projectDeptName1 = deptProjectService.getProjectDeptName(item.getSysId(), 2, 1);
            setState(item,1,1);
            item.setZerenDept(projectDeptName1);

        }

        return list;
    }

    /**
     * 带单位过滤
     * @param filter
     * @return
     */
    public List<ZhengQianEntity> getLists(ZhengQianFilter filter){
        List<ZhengQianEntity> list = dao.getLists(filter);
        for (ZhengQianEntity item : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(item.getSysId(), 1, 1);
            item.setPeiheDept(projectDeptName);
            String projectDeptName1 = deptProjectService.getProjectDeptName(item.getSysId(), 2, 1);
            setState(item,1,1);
            item.setZerenDept(projectDeptName1);

        }
        return list;
    }

    public List<ZhengQianEntity> getListNoPage(ZhengQianFilter filter){


        List<ZhengQianEntity> list = dao.getListNoPage(filter);


        return list;
    }


}
